<?php
    include 'head.php';

	//取出banner数据
	$sql = "select name,cate_id,style from ".PRE."image where size='1' and `display`='1' limit 1";
	$result = mysql_query($sql);
	$bannerlist = mysql_fetch_assoc($result);

	//取出广告品牌信息
	$sql = "select name,cate_id,style from ".PRE."image where size='2' and `display`='1' limit 4";
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result)){
		$brandlist[] = $row;
	}

	//取出要显示的板块
	$sql = "select cate_id,style from ".PRE."homepage";
	$result = mysql_query($sql); 
	while($row = mysql_fetch_assoc($result)){
		$showlist[] = $row;
	}
	mysql_free_result($result);
?>

<!-- 这里编辑首页的主体内容 -->
	<div id="shouye_main" class="w">
		<div class="banner">
		<a href="./secondary.php?cid=<?php echo $bannerlist['cate_id']?>&st=<?php echo $bannerlist['style']?>"><img src="./upload/<?php echo $bannerlist['name']?>"></a>
		</div>
		<!-- 这里首页上面的品牌展示 -->
        <?php if(!empty($brandlist)):?>
		<div class="brands">
			<img src="./images/x_t.jpg"/>
			<ul>
				<?php foreach($brandlist as $val):?>
				<!-- 广告图片偶数向左浮动，奇数向右浮动 -->
				<li class="<?php $i++; echo $i%2 == 0?'fl':'fr';?>"><a href="./secondary.php?cid=<?php echo $val['cate_id']?>&st=<?php echo $val['style']?>"><img src="./upload/<?php echo $val['name']?>" alt="" /></a></li>
				<?php endforeach;?>
				<div class="clear"></div>
			</ul>
        </div>
        <?php endif;?>
		<!-- 品牌展示结束 -->
		<?php foreach($showlist as $show):?>
			<div class="good_list mt20">
				<?php 
					$cate = $show['cate_id'];
					$style = $show['style'];
					//取出所需数据，banner图和商品数组
					$sql = "select name,cate_id,style from ".PRE."image where size='1' and cate_id='{$cate}' and style='{$style}'";
					$banner = mysql_fetch_assoc(mysql_query($sql));
					//$banner 是一个一维数组，里面三个元素，name cate_id ，style

					//取出商品数据
					$sql = "select id,cate_id,name,price,image,addtime from ".PRE."goods where (cate_id in (select id from ".PRE."category where path like '%,{$cate},%') || cate_id = '{$cate}') and style='{$style}' and is_up='1' order by addtime desc limit 5";
					$result = mysql_query($sql);
					$goodslist = array();
					while($row = mysql_fetch_assoc($result)){
						$goodslist[] = $row;
					}
					mysql_free_result($result);
					//$goodslist 是一个二维数组，遍历取出里面的商品信息，包含id,cate_id,name,price,image字段	
				?>	
				
				<div class="title">
				<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>" class="fl fontyh"><span><?php 
					$sql = "select name from ".PRE."style where id='{$banner['style']}'";
					$list = mysql_fetch_assoc(mysql_query($sql));
					echo $list['name'];	
					echo '-';
					$sql = "select name from ".PRE."category where id='{$banner['cate_id']}'";
					$list = mysql_fetch_assoc(mysql_query($sql));
					echo $list['name'];
				?><span></a>
					<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>" class="fr"> 更多 >></a>
					<div class="clear"></div>
				</div>
				<div>
					<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>"><img src="./upload/<?php echo $banner['name'] ?>" alt="" /></a>
				</div>
				<div class="sub_list mt20">
					<ul>
						<?php foreach($goodslist as $val): ?>
						<li class="fl">
							<div class="pic">
								<a href="./detail.php?gid=<?php echo $val['id']?>&cid=<?php echo $val['cate_id']?>"><img src="./upload/<?php echo $val['image']?>" width="235" alt="" /></a>
							</div>
							<div class="clear"></div>
							<div class="name_price">
								<a href="./detail.php?gid=<?php echo $val['id']?>&cid=<?php echo $val['cate_id']?>"><?php echo $val['name'] ?></a><br />
								<span class="newprice fontyh">￥<?php echo $val['price']?></span>
								<span>市场价：￥<?php echo ceil($val['price']/0.7) ?></span>
							</div>
						</li>
						<?php endforeach; ?>
						<div class="clear"></div>
					</ul>
				</div>
			</div>
		<?php endforeach;?>
	
	
	</div>



<?php
    include 'footer.php';
?>
